
#include "smartled.h"


int SMARTLED::ledAttach(int pin){
  ledPin = pin;
  pinMode(pin, OUTPUT);
  return 0;
}

int SMARTLED::ledSetMode(int mode, int time_on = 1000, int time_off=1000){

  switch(mode){
  case 0: // off

    digitalWrite(ledPin,LOW);
    break;

  case 1:
    digitalWrite(ledPin,HIGH);
    break;

  case 2:
    if (ledTimeOn > millis()) ledTimeOn = millis();
    if (ledTimeOff > millis()) ledTimeOff = millis();

    if (ledMode == 2) {                     // previous mode was blink
      if (digitalRead(ledPin) == HIGH){
        if((millis() - ledTimeOn) > time_on){
          //if (DEBUG) Serial.print("switch off " );                       
          //if (DEBUG) Serial.println(ledTimeOn);
          digitalWrite(ledPin,LOW);
          //          ledTimeOn = 0;
          ledTimeOff = millis();

        }
      }
      else{
        if((millis() - ledTimeOff) > time_off){
          //if (DEBUG) Serial.print("switch on " );                       
          //if (DEBUG) Serial.println(ledTimeOff);
          digitalWrite(ledPin,HIGH);
          ledTimeOff = millis();
          ledTimeOn = millis();
        }
      }
    }
    else{//init blinking cicle
      ledMode=2;
      ledTimeOn = millis();
      digitalWrite(ledPin,HIGH);
    }
  }
  return 0;
}

